/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package smarthouse;
import java.awt.Graphics;
import java.awt.Label;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author shen, martin
 */
public class Outdoors extends javax.swing.JFrame {

    /**
     * Creates new form ImageApp
     */
    
        private static String serverAddress="192.168.1.106";
        private static int port=2345;
    //sever address
    public Outdoors() {
        initComponents();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
         setLocationRelativeTo(null);
//         setSize(400, 400);
          setResizable(false);
           getContentPane().setLayout(null);
          JPanel panel = new ImagePanel();
//          JLabel label = new ImageLabel();
          panel.setBounds(0, 0, 1024, 768);
//          label.setBounds(0, 0, 100, 100);
           getContentPane().add(panel);
//           getContentPane().add(label);


                 setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(1086, 880));
        setPreferredSize(new java.awt.Dimension(1086, 880));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smarthouse/images/windows1.jpg"))); // NOI18N

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smarthouse/images/windows2.jpg"))); // NOI18N

        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smarthouse/images/windows2.jpg"))); // NOI18N

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smarthouse/images/door_close.jpg"))); // NOI18N

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/smarthouse/images/light_off.jpg"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel5)
                .addGap(83, 83, 83)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3))
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(181, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(124, 124, 124))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(31, 31, 31)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String text =jTextField1.getText();
        if(text.equals("wo_on!")){
            jLabel1.setIcon(new javax.swing.ImageIcon("windows1.jpg"));
            jLabel2.setIcon(new javax.swing.ImageIcon("windows2.jpg"));
            jLabel3.setIcon(new javax.swing.ImageIcon("windows2.jpg"));
        }
        if(text.equals("wo_off!")){
            jLabel1.setIcon(new javax.swing.ImageIcon("windows1_close.jpg"));
            jLabel2.setIcon(new javax.swing.ImageIcon("windows2_close.jpg"));
            jLabel3.setIcon(new javax.swing.ImageIcon("windows2_close.jpg"));
        }
        if(text.equals("do_on!")){
            jLabel4.setIcon(new javax.swing.ImageIcon("door_open.jpg"));
        }if(text.equals("do_off!")){
            jLabel4.setIcon(new javax.swing.ImageIcon("door_close.jpg"));
        }if(text.equals("lo_on!")){
            jLabel5.setIcon(new javax.swing.ImageIcon("light_on.jpg"));
        }
        if(text.equals("lo_off!")){
            jLabel5.setIcon(new javax.swing.ImageIcon("light_off.jpg"));
        }
        if(text.equals("li_on!")){
            jLabel1.setIcon(new javax.swing.ImageIcon("windows1_light.jpg"));
            jLabel2.setIcon(new javax.swing.ImageIcon("windows2_light.jpg"));
            jLabel3.setIcon(new javax.swing.ImageIcon("windows2_light.jpg"));
        }
        if(text.equals("li_off!")){
            jLabel1.setIcon(new javax.swing.ImageIcon("windows1.jpg"));
            jLabel2.setIcon(new javax.swing.ImageIcon("windows2.jpg"));
            jLabel3.setIcon(new javax.swing.ImageIcon("windows2.jpg"));
        }
       
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
        // TODO add your handling code here:
//        String text = ((JButton)evt.getSource()).getText();
//        if(text.equals("1")){
//            jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\shen\\Documents\\NetBeansProjects\\smarthouse\\images\\1.jpg"));
//        }
//        if(text.equals("2")){
//            jLabel2.setIcon(new javax.swing.ImageIcon("C:\\Users\\shen\\Documents\\NetBeansProjects\\smarthouse\\images\\tv2.png"));
//        }
        
    }//GEN-LAST:event_jTextField1KeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws IOException {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Outdoors.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Outdoors.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Outdoors.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Outdoors.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
         Outdoors receiver=new Outdoors();
	 
         Socket socket=new Socket(serverAddress,port);  
   try{
   System.out.println("socket = "+socket);
     BufferedReader reader = 
       new BufferedReader(
          new InputStreamReader(
            socket.getInputStream()));
   PrintWriter writer = 
       new PrintWriter(
           new BufferedWriter(
              new OutputStreamWriter(
                 socket.getOutputStream())),true);
  BufferedReader localreader = new BufferedReader(new InputStreamReader(System.in));
    
String msg = null;
msg=localreader.readLine();
 writer.println(msg);
  System.out.println("from server:"+reader.readLine());
if(msg.equals("wo_on!")){
            receiver.jLabel1.setIcon(new javax.swing.ImageIcon("windows1.jpg"));
            receiver.jLabel2.setIcon(new javax.swing.ImageIcon("windows2.jpg"));
            receiver.jLabel3.setIcon(new javax.swing.ImageIcon("windows2.jpg"));
        }
        if(msg.equals("wo_off!")){
            receiver.jLabel1.setIcon(new javax.swing.ImageIcon("windows1_close.jpg"));
            receiver.jLabel2.setIcon(new javax.swing.ImageIcon("windows2_close.jpg"));
            receiver.jLabel3.setIcon(new javax.swing.ImageIcon("windows2_close.jpg"));
        }
        if(msg.equals("do_on!")){
            receiver.jLabel4.setIcon(new javax.swing.ImageIcon("door_open.jpg"));
        }if(msg.equals("do_off!")){
            receiver.jLabel4.setIcon(new javax.swing.ImageIcon("door_close.jpg"));
        }if(msg.equals("lo_on!")){
            receiver.jLabel5.setIcon(new javax.swing.ImageIcon("light_on.jpg"));
        }
        if(msg.equals("lo_off!")){
            receiver.jLabel5.setIcon(new javax.swing.ImageIcon("light_off.jpg"));
        }
        if(msg.equals("li_on!")){
            receiver.jLabel1.setIcon(new javax.swing.ImageIcon("windows1_light.jpg"));
            receiver.jLabel2.setIcon(new javax.swing.ImageIcon("windows2_light.jpg"));
            receiver.jLabel3.setIcon(new javax.swing.ImageIcon("windows2_light.jpg"));
        }
        if(msg.equals("li_off!")){
            receiver.jLabel1.setIcon(new javax.swing.ImageIcon("windows1.jpg"));
            receiver.jLabel2.setIcon(new javax.swing.ImageIcon("windows2.jpg"));
            receiver.jLabel3.setIcon(new javax.swing.ImageIcon("windows2.jpg"));
        }
//while((msg=localreader.readLine())!=null){
//  writer.println(msg);
//  System.out.println("from server:"+reader.readLine());
//  
//      
// 
// if(msg.equals("end")) 
//break;}
}catch(IOException ex){
ex.printStackTrace();
}finally{
System.out.println("");
try{
socket.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
        
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Outdoors().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);

ImageIcon icon = new ImageIcon("123.jpg");
g.drawImage(icon.getImage(), 0, 0, 1024, 768, this);
}
}

//class ImageLabel extends JLabel{
//public void paint(Graphics g) {
//super.paint(g);
//
//ImageIcon icon = new ImageIcon("C:\\Users\\shen\\Pictures\\1.jpg");
//g.drawImage(icon.getImage(), 0, 0, 100, 100, this);
//}
}

